#include <stdio.h>

#include <jni.h>
#include <string>
#include <string.h>
#include <android/log.h>
#include "book.h" //android 中定义的 .h 引用 用"" 否则编译引用报错

#define TAG "native-lib-sturct.cpp=="
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,TAG ,__VA_ARGS__)    // 定义LOGD类型

Books initBooks();

extern "C"
JNIEXPORT jlong JNICALL
Java_com_jniproject_StructActivity_newBookJni(JNIEnv *env, jclass instance) {
    Books Book1 = initBooks();

    LOGD("newBookJni %p", (&Book1));

    return (jlong)(&Book1);
}

Books initBooks(){
    struct Books Book1;        /* 声明 Book1，类型为 Book */
    /* Book1 详述 */
    strcpy( Book1.title, "C Programming");
    strcpy( Book1.author, "Nuha Ali");
    strcpy( Book1.subject, "C Programming Tutorial");
    Book1.book_id = 6495407;
    Book1.prices[0] = 1.1;
    Book1.prices[1] = 2.2;
    return Book1;
}

extern "C"
JNIEXPORT jlong JNICALL
Java_com_jniproject_StructActivity_newBookJni1(JNIEnv *env, jclass instance,jobject jobject123) {
    LOGD("newBookJni1 ");
    return 1.1L;
}
extern "C"
JNIEXPORT jobject JNICALL
Java_com_jniproject_StructActivity_getBook(JNIEnv *env, jclass instance) {

    jclass myClass = env->FindClass("com/jniproject/BookJava");
    // 获取类的构造函数，记住这里是调用无参的构造函数
    jmethodID id = env->GetMethodID(myClass, "<init>", "()V");
    // 创建一个新的对象
    jobject book = env->NewObject(myClass, id);

    jfieldID name = env->GetFieldID(myClass, "title", "Ljava/lang/String;");
    jfieldID book_id = env->GetFieldID(myClass, "book_id", "I");
    jfieldID bookPriceFloat = env->GetFieldID(myClass, "bookPriceFloat", "F");
//    jfieldID bookPriceFloat = env->GetFieldID(myClass, "bookPriceFloat", "Fjava/lang/Float");//java.lang.NoSuchFieldError: no field with name='bookPriceFloat' signature='Fjava/lang/Float' in class Lcom/jniproject/BookJava;

    env->SetObjectField(book, name, env->NewStringUTF("liuwei"));
    env->SetIntField(book, book_id, 20);
    env->SetFloatField(book, bookPriceFloat, 20.123);



/*    Books book2 = initBooks();
    LOGD("initBooks ");
    jfloatArray faceFeatureArray = env->NewFloatArray(book2.prices.length);
    LOGD("faceFeatureArray ");
    env->SetFloatArrayRegion(faceFeatureArray, 0, book2.prices.length, book2.prices);
    LOGD("SetFloatArrayRegion");
    jmethodID methodid = env->GetMethodID(myClass,"setBookPriceFloatS","([F)V");
    LOGD("GetMethodID");
//    for (int i = 0; i < faceFeatureArray.length; ++i) {
//        faceFeatureArray[i] = book1.prices[i];
//    }
    //4 调用方法
    (*env).CallVoidMethod(book,methodid,faceFeatureArray);
    LOGD("newBookJni %p", (&Book1));*/
    return book;
}
//    Books lm = initBooks();
//    jobject facePos = env->NewObject(Books.clazz, gCachedFacePosIDs.ctor);
//    env->SetFloatField(facePos, gCachedFacePosIDs.left, lm->left);
//    env->SetFloatField(facePos, gCachedFacePosIDs.top, lm->top);
//    env->SetFloatField(facePos, gCachedFacePosIDs.right, lm->right);
//    env->SetFloatField(facePos, gCachedFacePosIDs.bottom, lm->bottom);
//    env->SetFloatField(facePos, gCachedFacePosIDs.score, lm->score);
//    env->SetFloatField(facePos, gCachedFacePosIDs.roll, lm->roll);
//    env->SetFloatField(facePos, gCachedFacePosIDs.yaw, lm->yaw);
//    env->SetFloatField(facePos, gCachedFacePosIDs.pitch, lm->pitch);
//    return facePos;
//}
//    Java_com_jniproject_StructActivity_getBook(JNIEnv *env, jclass instance, jlong handle) {
//
//


